package com.bgs.travelapp.area.controller;

import com.bgs.travelapp.area.service.AreaService;
import com.bgs.travelapp.result.CommonResult;
import com.bgs.travelapp.util.MailUtils;
import com.bgs.travelapp.util.RedisUtil;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

/**
 * gpd 2022-07-04 19:44:00
 */

/**
 * hbx 2022-07-05 9:26:00
 * 景区管理
 */
@RestController
@CrossOrigin
@RequestMapping("/area")
public class AreaController {
    //将area接口注入到Bean容器中
    @Resource
    AreaService areaService;

    @Resource
    RedisUtil redisUtil;



    /**
     * 查询所有省份
     * 前端展示页--旅游
     * gpd 2022-07-05 11:26:00
     * @return
     */
    @GetMapping("/getProvinceAll")
    public CommonResult getProvinceAll(){
        Object areaAll = redisUtil.get("areaAll");
        if(areaAll==null){
            Map<Object, Object>  areaAllList = areaService.getProvinceAll();
            areaAll = redisUtil.set("areaAll",areaAllList);
        }
        areaAll = redisUtil.get("areaAll");
        return CommonResult.ok(areaAll);
    }

    /**
     * 鼠标移入省份时查询所有市区
     * 前端展示页--旅游
     * 根据pid进行查询
     * gpd 2022-07-05 10:26:00
     * @return
     */
    @GetMapping("/getCityAllByPid")
    public CommonResult getCityAllByPid(Integer pid){
        return CommonResult.ok(areaService.getCityAllByPid(pid));
    }

    /**
     * 全查城市树(后端树展示)
     * hbx 2022-07-05 09:26:00
     * @return
     */
    @GetMapping("/getAreaTreeAll")
    public CommonResult getAreaTreeAll(){
        Object areasAllTree = redisUtil.get("areasTreeAll");
        if(areasAllTree==null) {
            Map<Object, Object>  areaAllList = areaService.getAreaTreeAll();
            redisUtil.set("areasTreeAll", areaAllList);
        }
        areasAllTree = redisUtil.get("areasTreeAll");
        return CommonResult.ok(areasAllTree);
    }

    /**
     * 获取地区根据Pid
     * hbx 2022-7-12 19:30:53
     * @param pId
     * @return
     */
    @GetMapping("/getNameByPid")
    public CommonResult getNameByPid(Integer pId){
        return CommonResult.ok(areaService.getNameByPid(pId));
    }


    /**
     * 根据首字母获取市（前台展示）
     * lzy 2022-7-15 08点37分
     * todo 白
     * @param first
     * @return
     */
    @GetMapping("/getCitysByFirst")
    public CommonResult getCitysByFirst(String first){
        return CommonResult.ok(areaService.getCitysByFirst(first));
    }

    /**
     * 根据城市id查询城市名称 （前端 天气）
     * hbx 2022年8月24日15:21:54
     * @param cityId
     * @return
     */
    @GetMapping("/getCityNameByCityId")
    public CommonResult getCityNameByCityId(Integer cityId){
        return CommonResult.ok(areaService.getCityNameByCityId(cityId));
    }


}
